<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑' : '新增'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout" >
        <a-form-item label="社会组织名称">
          <a-select
            mode="multiple"
            showArrow
            placeholder="请选择社会组织名称"
            option-label-prop="label"
            :filterOption="filterOption"
            v-decorator="[
              'leader',
              {rules: [{ required: true, message: '请选择社会组织名称'}]}
            ]" >
            <a-select-option v-for="(item,index) in socialOrganizationTypeList" :key="index" :value="item.user_id" :label="item.user_name">{{ item.user_name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { socialOrganizationTypeList } from '@/api/case'
// 表单字段
const fields = ['no', 'description', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      socialOrganizationTypeList: [] // 社会组织名称列表
    }
  },
  created () {
    console.log('custom modal created')
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  mounted () {
    this.getSocialOrganizationTypeList()
  },
  methods: {
    // 社会组织名称列表
    getSocialOrganizationTypeList () {
      socialOrganizationTypeList().then(res => {
        // console.log(res)
        this.socialOrganizationTypeList = res
      })
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      )
  }
  }
}
</script>
